package cn.biplam.front.template.control;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.KvMap;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.htmlscript.script.FlowTableScript;

/**
 *  流式布局表行刷新模板
 * @author XiaoRuxing Oct 29, 200912:03:55 PM
 * @version 1.0
 */
public class FlowTableRefresh {
	
	/** 表刷新操作类型 */
	//刷新行
	private static final String EDIT = "1";
	//添加行
	private static final String ADD = "2";
	/** 刷新类型参数名称 */
	private static final String REFRESH_TYPE_CONTAINER = ".rType";
	
	/** 行内容 */
	private HtmlWindow rowContent;
	private String cId;
	private KvMap<String, String> params;
	private String rowIndex;
	
	/**
	 * 创建表格行刷新模板
	 * @param rowContent 表格行内容
	 * @param 被刷新行或者表格ID
	 * @param pramas     行刷新参数(行刷新类型)
	 */
	public FlowTableRefresh(HtmlWindow rowContent,
			String cId,
			KvMap<String, String> params){
		this.rowContent = rowContent;
		this.cId = cId;
		this.params = params;
	}
	
	/**
	 *  设置被刷新行在表格中的位置
	 * @param rowIndex 行索引
	 */
	public void setRowIndex(String rowIndex){
		this.rowIndex = rowIndex;
	}
	
	/**
	 *  获取行刷新脚本
	 */
	public HtmlWindow get(String tableId) throws ErrorMessage{
		String opType = params.getByKey(REFRESH_TYPE_CONTAINER);
		String script=null;
		if(EDIT.equals(opType)){
			if(tableId==null){
				script = FlowTableScript.refreshRow(this.cId, this.rowContent);
			}else{
				script = FlowTableScript.refreshRow(this.cId, this.rowContent,tableId);
			}
		}else if(ADD.equals(opType)){
			script = FlowTableScript.addRow(this.cId, this.rowContent, rowIndex);
		}
		HtmlDiv div = new HtmlDiv();
		div.bind(script);
		return div;
	}
	
	/**
	 *  获取行刷新脚本
	 */
	public HtmlWindow get() throws ErrorMessage{
		return get(null);
	}
}
